package com.seocoo.worker.order;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.seocoo.worker.R;
import com.sfh.lib.ui.MVVMFragment;

import java.util.ArrayList;
import java.util.Objects;

/**
 * author : Ssp
 * date   : 2020/2/23 16:25
 * desc   : 维修订单列表-平台指派
 */
public class OrderStarMaintainParentFragment2 extends MVVMFragment {

    private final String[] mTitles = {"进行中", "已完成"};

    @Override
    public int getLayout() {
        return R.layout.order_maintain_parent_fragment2;
    }

    @Override
    public void initData(View view) {
        SlidingTabLayout tab_layout = view.findViewById(R.id.tab_layout2);
        ViewPager viewpager = view.findViewById(R.id.viwepager2);
        ArrayList<Fragment> list = getFragments();
        tab_layout.setViewPager(viewpager, mTitles, Objects.requireNonNull(getActivity()), list);
        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                refreshData(position, list);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        tab_layout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                refreshData(position, list);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        viewpager.setCurrentItem(0);
    }

    private void refreshData(int position, ArrayList<Fragment> list) {
        OrderMaintainFragment fragment = (OrderMaintainFragment) list.get(position);
        if (fragment != null) {
            fragment.reFreshData();
        }
    }

    private ArrayList<Fragment> getFragments() {
        ArrayList<Fragment> list = new ArrayList<>();
        Fragment fragment2 = OrderMaintainFragment.newInstance("2", "1");
        list.add(fragment2);
        Fragment fragment3 = OrderMaintainFragment.newInstance("3", "1");
        list.add(fragment3);
        return list;
    }

}
